<?php $this->loadHelper('common'); ?>
<div id="manager">
    <?php echo $this->renderElement('sidebar/video.tpl.php', array('_module' => $_module)); ?>
    <div class="right">
        <div id="content" style="border:1px solid #B0BFCC">
            <form name="frmSearch" action="/video/search" method="GET">
            <div class="area-button">
                <span>
                    Tìm kiếm theo
                    <select name="search_type">
                        <option value="name">Tên</option>
                        <option value="id">ID</option>
                    </select>
                    <input type="text" class="textbox" style="width:450px" name="keyword" value="<?php echo isset($keyword) ? htmlentities(strip_tags($keyword), ENT_QUOTES, "UTF-8") : ''; ?>" />
                    <a class="button" href="javascript:;" onclick="document.frmSearch.submit();return false;">Tìm</a>
                </span>
            </div>
            </form>
        </div>
        <br/>
        <div>
            <div id="bg-title1"><img src="/images/bg-title1.jpg" alt="" /></div>
            <div id="bg-title2a"><img src="/images/icon-title.jpg" style="margin-right:8px;" alt="" />Quản lý, kiểm duyệt video</div>

            <div id="bg-title3"><img src="/images/bg-title3.jpg" alt="" /></div>
            <div class="clearfloat"></div>
        </div>
        <div id="content">
            <div class="area-button">
                <div class="area-button1">
                    <?php if ($totalPage > 0): ?>
                    <div class="video1"><input type="checkbox" onclick="checkAllBox(this.id, 'content');" id="check-all"/></div>
                    <?php endif; ?>

                    <?php if ($type != 'deleted'): ?>
                    <a class="button" href="javascript:;" onclick="deleteObject('video', null, '<?php echo $_SESSION['token'];?>');return false;">Xóa</a>
                    <?php endif; ?>
                    
                    <script type="text/javascript">
                        function listByFilter() {
                            var val = (document.getElementById('filter').value);
                            var catId = document.getElementById('catFilter').value;
                            location.href = '/video/?type=' + val + '&catId=' + catId;
                        }
                    </script>

                    
                    <select style="float:left" name="catId" id="catFilter" onchange="listByFilter()">
                        <optgroup label="Lọc chuyên mục">
                            <?php foreach ($videoCats as $item): ?>
                            <?php $parentId = $item['id']; ?>
                            <?php if(!$item['parent_id']): ?>
                            <option<?php echo ($catId == $item['id']) ? ' selected' : ''; ?> value="<?php echo $item['id'] ?>"><?php echo $item['name']  ?></option>

                            <?php foreach ($videoCats as $item): ?>
                            <?php if($item['parent_id']==$parentId): ?>
                            <option<?php echo ($catId == $item['id']) ? ' selected' : ''; ?> value="<?php echo $item['id'] ?>">----- <?php echo $item['name']  ?></option>
                            <?php endif; ?>
                            <?php endforeach; ?>

                            <?php endif; ?>
                            <?php endforeach; ?>

                            <option<?php echo ($catId == 0) ? ' selected' : '' ?> value="0">Tất cả</option>
                        </optgroup>
                    </select>
                    
                    <select style="float:left" name="type" id="filter" onchange="listByFilter();">
                        <optgroup label="Lọc trạng thái">
                            <option <?php echo ($type == 'pending') ? 'selected' : ''; ?> value="pending" >Đang chờ duyệt</option>
                            <option <?php echo ($type == 'active') ? 'selected' : ''; ?> value="active" >Đã kích hoạt</option>
                            <option <?php echo ($type == 'deleted') ? 'selected' : ''; ?> value="deleted" >Đã bị xóa</option>
                            <option <?php echo ($type == 'notconvert') ? 'selected' : ''; ?> value="notconvert" >Chưa được convert</option>
                            <option <?php echo ($type == 'convert_fail') ? 'selected' : ''; ?> value="convert_fail">Convert lỗi</option>
                        </optgroup>
                    </select>
                </div>
                <div class="clearfloat"></div>
            </div>            
            <?php foreach ($videos as $video): ?>
                <?php
                $videoStatus = 'không rõ!';
                $converted  = $video['converted'];
                $active     = $video['active'];
                $deleted    = $video['deleted'];
                $updated_by_admin = ($video['member_id'] != $video['updated_by']);
				
                if ($converted==1 && $active==1 && $deleted==0) {
                    $videoStatus = 'đã kích hoạt';
                } else if ($converted==1 && $active==0 && $deleted==0) {
                        $videoStatus = 'đang chờ kích hoạt';
                    } elseif ($converted==1 && $active==1 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'bị từ chối (decline)';
                    } elseif ($converted==1 && $active==1 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'đã kích hoạt nhưng CP xóa';
                    } elseif ($converted==-1 && $deleted==0) {
                        $videoStatus = 'convert bị lỗi';
                    } elseif ($converted==-1 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'convert lỗi, đã bị quản trị xóa';
                    } elseif ($converted==-1 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'convert lỗi, đã bị CP xóa!';
                    } elseif ($converted==0 && $deleted==0) {
                        $videoStatus = 'chưa convert';
                    } elseif ($converted==0 && $deleted==1 && $updated_by_admin) {
                        $videoStatus = 'chưa convert đã bị quản trị xóa!';
                    } elseif ($converted==0 && $deleted==1 && !$updated_by_admin) {
                        $videoStatus = 'chưa convert đã bị CP xóa!';
                    } elseif ($converted==1 && $deleted==1 && $active==0 && $updated_by_admin) {
                        $videoStatus = 'đang chờ duyệt thì quản trị xóa!';
                    } elseif ($converted==1 && $deleted==1 && $active==0 && !$updated_by_admin) {
                        $videoStatus = 'đang chờ duyệt thì CP xóa!';
                    }
                ?>
            <div class="area-content1">
                <div class="video1"><input type="checkbox" id="item-<?php echo $video['id'] ?>" value="<?php echo $video['id'] ?>" /></div>
                <div class="video2">
                        <?php if($converted == 1): ?>
                            <?php //if($type!='pending'):?>
                    <a href="/video/edit?id=<?php echo $video['id'] ?>"><img src="<?php echo makeVideoThumbnailUrl($video['id'], $video['thumbnail_version']); ?>" width="128" height="96" alt="Ảnh thumbnail" /></a>
                            <?php //else: ?>
                    <!--img src="<?php //echo makeVideoThumbnailUrl($video['id'], $video['thumbnail_version']); ?>" width="128" height="96" alt="Ảnh thumbnail" /-->
                            <?php //endif; ?>
                        <?php else: ?>
                    <img src="/images/processing.jpg" alt="" width="128" height="96"/>
                        <?php endif; ?>
                </div>
                <div class="video3">
                        <?php if($type!='pending'):?>
                    <a href="/video/edit?id=<?php echo $video['id'] ?>" class="style_title"><?php echo $video['title']; ?></a> <?php if($video['hd']): ?><img src="/images/hd_icon.gif" /><?php endif;?> <?php if($video['source']=='nn'): ?><img src="/images/eng_icon.gif" /><?php endif;?>
                        <?php else: ?>
                            <?php echo $video['title']; ?>
                        <?php endif; ?>
                    <br />
                        <?php 
							echo truncate(strip_tags($video['description']), 150); ?><br />
                </div>
                <div class="video4">
                    Đăng ngày: <?php echo timeFormat($video['created_time']); ?><br />
                    <?php /** if($video['owner']): ?>
                    Đăng bởi: <b><?php echo $video['owner']; ?></b><br/>
                    <?php endif; ?>
                    <?php if($video['subcp_name']): ?>
                    Đối tác: <b><?php echo $video['subcp_name']; ?></b><br/>
                    <?php endif; ?>
                    <?php if($video['approver']): ?>
                    Duyệt bởi: <b><?php echo $video['approver']; ?></b><br/>
                    <?php endif; /**/?>
                    Giá xem: <?php echo $video['price']; ?><br />
                    Giá tải: <?php echo $video['download_price']; ?><br />
                    Chuyên mục: <?php echo $video['category_name']; ?><br/>
                    Trạng thái: <?php echo $videoStatus; ?><br />
                    Độ dài: <?php echo secondToTime($video['duration']); ?><br />
                    Xem: <?php echo $video['watch_count']; ?> - Tải: <?php echo $video['download_count']; ?> - Tặng: <?php echo $video['present_count']; ?><br/>
                </div>
                <div class="clearfloat"></div>

                <div class="video5">
                        <?php if (($type == 'pending' || $type == 'active' || $type=='deleted')
                            && $video['converted'] == 1): ?>
                    <a href="#" class="button1" onclick="showVideo(<?php echo $video['id']; ?>);">Xem clip</a>
                        <?php endif; ?>

                        <?php if ($type == 'pending' || $type == 'active' || $type=='notconvert' || $type=='convert_fail'): ?>
                    <a href="javascript:;" onclick="deleteObject('video',<?php echo $video['id']; ?>, '<?php echo $_SESSION['token'];?>');return false;" class="button1">Xóa clip</a>
                        <?php endif;?>

                        <?php //if ($type == 'active'): ?>
                    <a href="/video/edit?id=<?php echo $video['id']; ?>" class="button1">Sửa clip</a>
                        <?php //endif; ?>

                        <?php if ($type == 'pending'): ?>
                    <a href="javascript:;" onclick="approveVideo(<?php echo $video['id']; ?>);return false;" class="button1">Chọn duyệt</a>
                        <?php endif; ?>

                        <?php if ($type == 'convert_fail'): ?>
                    <a href="#" class="button1">Convert lại</a>
                        <?php endif;

                    if ($type == 'active' && !isset($featuredVideos[$video['id']])){?>
                        <a href="/featured_video/add?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào sanh sách chọn lọc">+Chọn lọc</a><?php 
                    }elseif($type == 'active' && isset($featuredVideos[$video['id']])){?>
                        <a href="/featured_video/delete?id=<?php echo $video['id']; ?>" class="button1" title="Bỏ ra danh sách chọn lọc">-Chọn lọc</a><?php
                    }
                    //if(isAdmin ()){
                        if(!isset($weekVideo[$video['id']])){?>
                            <a href="/week_video/add?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào bản tin video hot hàng tuần">+Weekly</a><?php
                        }else{?>
                            <a href="/week_video/delete?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào bản tin video hot hàng tuần">-Weekly</a><?php
                        }                    
                        if(!isset($hotVideo[$video['id']])){?>
                            <a href="/hot_video/add?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào bản tin video hot">+Daily</a><?php
                        }else{?>
                            <a href="/hot_video/delete?id=<?php echo $video['id']; ?>" class="button1" title="Thêm vào bản tin video hot">-Daily</a><?php
                        }
                    //}?>
                    <a href="/myvideo/statistic?id=<?php echo $video['id']; ?>" class="button1" title="Thống kê">Thống kê</a><?php
                    if(isAdmin ()){?>
                        <a href="/behavior?admin=&object=video&object_id=<?=$video['id']?>" class="button1" title="Thống kê">Lịch sử</a><?php
                    }?>
                </div>

                <div class="clearfloat"></div>
            </div>

            <?php endforeach; ?>
            <?php if ($totalPage != 0): ?>
            <div class="area-button">
                <div class="area-button1">
                        <?php if ($type != 'deleted'): ?>
                    <a href="javascript:;" onclick="deleteObject('video', null, '<?php echo $_SESSION['token'];?>');return false;" class="button">Xóa</a>
                        <?php endif; ?>
                </div>
                <div style="float:right; padding-right:10px">Có tất cả <b><?php echo $totalVideo; ?></b> video</div>
                <div class="clearfloat"></div>
            </div>
            <?php else: ?>
            <div class="area-content1">Không có video nào.</div>
            <?php endif; ?>
            <?php echo $this->renderElement('paging.tpl.php', array('page' => $curPage, 'totalPage' => $totalPage)); ?>
        </div>
    </div>
</div>
<div class="clearfloat"></div>

<script type="text/javascript">
    function showVideo(clipId) {
        $.get("/video/getplayer", { id: clipId }, function(data){
            $.modal(data);
        });
    }
</script>
